<?php

namespace app\official\controller;

use think\Controller;

class Index extends Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_bd568ce7058a1091"></thinkad>';
    }

    public function business()
    {
        $businessData = model('Business.Business')->select();

        echo json_encode(['code' => 1, 'msg' => '查询成功', 'data' => $businessData]);
        // $this->success('查询成功',null,$businessData);
    }

    public function login()
    {
        $code = $this->request->param('code', '', 'trim');

        $SessionData = $this->code2Session($code);

        if ($SessionData['code'] === 0) {
            echo json_encode(['code' => 0, 'msg' => $SessionData['msg'], 'data' => null]);
            exit;
        }

        $openid = $SessionData['data']['openid'];

        $Business = model('Business.Business')->where(['openid' => $openid])->find();

        if (!$Business) {
            echo json_encode(['code' => 0, 'msg' => '请先绑定账号', 'data' => null]);
            exit;
        }
    }

    protected  function code2Session($code)
    {
        $result = [
            'code' => 0,
            'msg' => '登录凭证未知',
            'data' => null,
        ];

        if ($code) {
            // 小程序appid(改成自己的)
            $appid = 'wx8de9d19aab6c401a';

            // 小程序密钥(改成自己的)
            $AppSecret = '1d5880383f8ed68f1561e1b7fe010d48';

            $apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$AppSecret&js_code=$code&grant_type=authorization_code";

            $res = $this->httpRequest($apiUrl);

            $res = json_decode($res, true);

            $result = [
                'code' => 1,
                'msg' => '获取登录凭证成功',
                'data' => $res
            ];

            return $result;
        }

        return $result;
    }

    protected function httpRequest($url, $data = null)
    {
        if (function_exists('curl_init')) {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
            if (!empty($data)) {
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $output = curl_exec($curl);
            curl_close($curl);
            return $output;
        } else {
            return false;
        }
    }
}
